//主进程
const { app, BrowserWindow, ipcMain, Menu } = require('electron')
const path = require('node:path')
let bookData = ''

const createWindow = () => {
    const win = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
            preload: path.join(__dirname, 'preload.js'),
            nodeIntegration: true,//是否启用Node integration. 默认值为 false
            enableRemoteModule: true 
        }
    })
    ipcMain.on('open-dialog', (event, data) => {
        bookData = data
        createDialog()
    })
    win.webContents.openDevTools()
    win.loadFile('./views/home/index.html')
}

app.whenReady().then(() => {
    createWindow()
    app.on('activate', () => {
        if (BrowserWindow.getAllWindows().length === 0) createWindow()
    })
})
//所有窗口关闭后退出应用（Windows 和 Linux）
app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') app.quit()
})

// 新建窗口
function createDialog() {
    const childWin = new BrowserWindow({
        width: 400,
        height: 120,
        webPreferences: {
            preload: path.join(__dirname, 'preload.js'),
        },
        frame: false,
        transparent: true 
    })
    childWin.loadFile("./views/child/child.html")
    childWin.on('ready-to-show', () => {
        childWin.webContents.send('load-text',bookData)
    })
}
